package com.example.javaow.service.impl;/*
 * @Auther:Sadie
 * @Date:2025/8/12
 * @Description:
 * @VERSON:1.8
 */

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.example.javaow.domain.base.ResponseResult;
import com.example.javaow.domain.dto.DeviceGroupDto;
import com.example.javaow.domain.entity.DeviceGroup;
import com.example.javaow.domain.vo.DeviceGroupVo;
import com.example.javaow.mapper.DeviceGroupMapper;
import com.example.javaow.service.DeviceGroupService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class DeviceGroupGroupServiceImpl implements DeviceGroupService {
    @Autowired
    private DeviceGroupMapper deviceGroupMapper;

    // 添加
    @Override
    @Transactional
    public ResponseResult add(DeviceGroupDto deviceGroupDto) {
        DeviceGroup deviceGroup = new DeviceGroup();
        BeanUtils.copyProperties(deviceGroupDto,deviceGroup);
        deviceGroup.setCreatetime(LocalDateTime.now());
        if(deviceGroupMapper.insert(deviceGroup)>0)
            return ResponseResult.success("添加成功");
        return ResponseResult.error("添加失败");
    }

    // 修改
    @Override
    public ResponseResult update(DeviceGroupDto deviceGroupDto) {
        DeviceGroup deviceGroup = new DeviceGroup();
        BeanUtils.copyProperties(deviceGroupDto, deviceGroup);
        deviceGroup.setUpdatetime(LocalDateTime.now());
        if (deviceGroupMapper.updateById(deviceGroup) > 0) {
            return ResponseResult.success("更新成功");
        }
        return ResponseResult.error("更新失败或记录不存在");
    }

    // 删除（支持批量）
    @Override
    public ResponseResult delete(Long[] ids) {
        if (ids == null || ids.length == 0) {
            return ResponseResult.error("请选择要删除的分组");
        }
        // 可选：检查是否存在子分组（防止误删）
        List<Long> idList = Arrays.asList(ids);
        LambdaQueryWrapper<DeviceGroup> wrapper = Wrappers.<DeviceGroup>lambdaQuery()
                .in(DeviceGroup::getParentId, idList);
        Long childCount = deviceGroupMapper.selectCount(wrapper);
        if (childCount > 0) {
            return ResponseResult.error("存在子分组，无法删除父分组");
        }

        int result = deviceGroupMapper.deleteBatchIds(Arrays.asList(ids));
        if (result > 0) {
            return ResponseResult.success("删除成功");
        }
        return ResponseResult.error("删除失败");
    }

    // 根据 ID 查询
    @Override
    public ResponseResult getById(Long id) {
        DeviceGroup deviceGroup = deviceGroupMapper.selectById(id);
        if (deviceGroup != null) {
            DeviceGroupVo vo = new DeviceGroupVo();
            BeanUtils.copyProperties(deviceGroup, vo);
            return ResponseResult.success(vo);
        }
        return ResponseResult.error("未找到该分组");
    }

    // 查询全部
    @Override
    public ResponseResult getAll() {
        LambdaQueryWrapper<DeviceGroup> wrapper = Wrappers.<DeviceGroup>lambdaQuery()
                .isNull(DeviceGroup::getParentId);
        List<DeviceGroup> list = deviceGroupMapper.selectList(wrapper);
        List<DeviceGroupVo> voList = list.stream().map(item -> {
            DeviceGroupVo vo = new DeviceGroupVo();
            BeanUtils.copyProperties(item, vo);
            return vo;
        }).collect(Collectors.toList());
        return ResponseResult.success(voList);
    }

    // 条件查询：根据 parent_id 和 name（模糊）
    @Override
    public ResponseResult getByCondition(Long parentId, String name) {
        LambdaQueryWrapper<DeviceGroup> wrapper = Wrappers.lambdaQuery();
        if(parentId!= null)
            wrapper.eq(DeviceGroup::getParentId, parentId);
        if(name != null)
            wrapper.like(DeviceGroup::getName, name);
        List<DeviceGroup> list = deviceGroupMapper.selectList(wrapper);
        List<DeviceGroupVo> voList = list.stream().map(item -> {
            DeviceGroupVo vo = new DeviceGroupVo();
            BeanUtils.copyProperties(item, vo);
            return vo;
        }).collect(Collectors.toList());
        return ResponseResult.success(voList);
    }
}
